
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class JKLocalizations {
  final Locale locale;

  JKLocalizations(this.locale);

  static JKLocalizations of(BuildContext context) {
    return Localizations.of(context, JKLocalizations);
  }

  /*
  static Map<String, Map<String, String>> _localizedValues = {
    "en": {
      "title": "home",
      "greet": "hello~",
      "picktime": "Pick a Time"
    },
    "zh": {
      "title": "首页",
      "greet": "你好~",
      "picktime": "选择一个时间"
    }
  };
  */

  static Map<String, Map<String, String>> _localizedValues = {};

  // 获取配置的 Map
  Future<bool> loadJson() async {
    // 1.加载json文件
    String jsonString = await rootBundle.loadString("assets/json/i18n.json");

    print('---------$jsonString----------');

    // 2.转成map类型
    final Map<String, dynamic> map = json.decode(jsonString);

    // 3.注意：这里是将Map<String, dynamic>转成Map<String, Map<String, String>>类型
    _localizedValues = map.map((key, value) {
      return MapEntry(key, value.cast<String, String>());
    });
    return true;
  }

  String get title {
    return _localizedValues[locale.languageCode]["title"];
  }

  String get greet {
    return _localizedValues[locale.languageCode]["greet"];
  }

  String get pickTime {
    return _localizedValues[locale.languageCode]["picktime"];
  }
}